<?php
/**
 * <pre>
 * DiabloStudios.Board v1.0
 * 
 * Main initialization file
 * </pre>
 * 
 * @copyright	(c) 2010 DiabloStudios
 * @license		GNU Affero GPL v3 or newer
 * @package		PHP-Fusion
 * @subpackage	DiabloStudios.Board
 * @link		http://diablostudios.pl
 * @version     $Id: main.php 1 2011-03-20 11:56:30Z Bartek $
 */
if (!defined("IN_FUSION")) die('Access Denied!');
error_reporting(E_ALL & ~ E_NOTICE);

define('DSB_DIR', INFUSIONS . "ds_board/");
define('DSB_ADIR', DSB_DIR . "admin/");
define('DSB_FDIR', DSB_DIR . "forum/");

define("TIME_NOW", time());

define("DB_DSB_ANNOUNCEMENTS", DB_PREFIX . "dsb_announcements");
define("DB_DSB_DATACACHE", DB_PREFIX . "dsb_datacache");
define("DB_DSB_FORUMPERMS", DB_PREFIX . "dsb_forumpermissions");
define("DB_DSB_FORUMS", DB_PREFIX . "dsb_forums");
define("DB_DSB_FORUMSREAD", DB_PREFIX . "dsb_forumsread");
define("DB_DSB_FORUMSUBSCRIPTIONS", DB_PREFIX . "dsb_forumsubscriptions");
define("DB_DSB_MODERATORS", DB_PREFIX . "dsb_moderators");
define("DB_DSB_MODTOOLS", DB_PREFIX . "dsb_modtools");
define("DB_DSB_SETTINGGROUPS", DB_PREFIX . "dsb_settinggroups");
define("DB_DSB_SETTINGS", DB_PREFIX . "dsb_settings");
define("DB_DSB_THREADR",DB_PREFIX . "dsb_threadratings");
define("DB_DSB_THREADS", DB_PREFIX . "dsb_threads");
define("DB_DSB_THREADSP", DB_PREFIX . "dsb_threadprefixes");
define("DB_DSB_THREADSR", DB_PREFIX . "dsb_threadsread");
define("DB_DSB_USERGROUPS", DB_PREFIX . "dsb_usergroups");
define("DB_DSB_USERS", DB_PREFIX . "dsb_users");



require_once DSB_DIR . "includes/functions.php";

require_once DSB_DIR . "includes/core.php";
$dsb = new DS_Board;

require_once DSB_DIR . "includes/datacache.php";
$cache = new datacache;

// Load Settings
if (file_exists(DSB_DIR . "includes/settings.php"))
{
	require_once DSB_DIR . "includes/settings.php";
}

if (!file_exists(DSB_DIR . "includes/settings.php") || !$settings)
{
	if (function_exists('rebuild_settings'))
	{
		rebuild_settings();
	}
	else
	{
		$query = dbquery("SELECT name,value FROM " . DB_DSB_SETTINGS . " ORDER BY name ASC");
		while ($setting = dbarray($query))
		{
			$setting['value'] = str_replace("\"", "\\\"", $setting['value']);
			$settings[$setting['name']] = $setting['value'];
		}
	}
}

if (is_dir(DSB_DIR . "locales/" . LOCALESET))
{
	define("DSB_LANGDIR", DSB_DIR . "locales/" . LOCALESET);
}
else
{
	define("DSB_LANGDIR", DSB_DIR . "locales/English/");
}

require_once DSB_LANGDIR . "global.php";

$settings['wolcutoff'] = $settings['wolcutoffmins'] * 60;

$dsb->settings = &$settings;
$cache->cache();
$dsb->parse_cookies();
$dsb->cache = &$cache;

if ($dsb->settings['dsb_on'] != '0') {
    define("DSB_ON",TRUE);
} else {
    define("DSB_ON",FALSE);
}

if (iMEMBER)
{
	$dsb->user = &$userdata;
}

require_once DSB_DIR . "includes/templates.php";
$templates = new templates;
$templates->tpl_path = DSB_DIR . "templates/";

if ($dsb->user['user_id'])
{
	// Check if this user is currently banned and if we have to lift it.
	if (!empty($dsb->user['bandate']) && (isset($dsb->user['banlifted']) && !empty($dsb->user['banlifted'])) && $dsb->user['banlifted'] < $time) // hmmm...bad user... how did you get banned =/

	{
		// must have been good.. bans up :D
		$db->shutdown_query("UPDATE " . TABLE_PREFIX . "users SET usergroup='" . intval($dsb->user['banoldgroup']) . "', additionalgroups='" . $dsb->user['oldadditionalgroups'] . "', displaygroup='" . intval($dsb->user['olddisplaygroup']) . "' WHERE uid='" . $dsb->user['uid'] . "' LIMIT 1");
		$db->shutdown_query("DELETE FROM " . TABLE_PREFIX . "banned WHERE uid='" . $dsb->user['uid'] . "'");
		// we better do this..otherwise they have dodgy permissions
		$dsb->user['usergroup'] = $dsb->user['banoldgroup'];
		$dsb->user['displaygroup'] = $dsb->user['banolddisplaygroup'];
		$dsb->user['additionalgroups'] = $dsb->user['banoldadditionalgroups'];
		$cache->update_banned();

		$dsbgroups = $dsb->user['usergroup'];
		if ($dsb->user['additionalgroups'])
		{
			$dsbgroups .= ',' . $dsb->user['additionalgroups'];
		}
	} elseif (!empty($dsb->user['bandate']) && (empty($dsb->user['banlifted']) || !empty($dsb->user['banlifted']) && $dsb->user['banlifted'] > $time))
	{
		$dsbgroups = $dsb->user['usergroup'];
	}
	else
	{
		// Gather a full permission set for this user and the groups they are in.
		$dsbgroups = $dsb->user['usergroup'];
		if ($dsb->user['additionalgroups'])
		{
			$dsbgroups .= ',' . $dsb->user['additionalgroups'];
		}
	}

	$dsb->usergroup = usergroup_permissions($dsbgroups);
}
else
{
	$dsb->usergroup = usergroup_permissions('1');
}

if ($dsb->settings['seourls'] == "yes" || ($dsb->settings['seourls'] == "auto" && $_SERVER['SEO_SUPPORT'] == 1))
{
    define('FORUM_URL_PAGED','forum-{fid}-page-{page}.html');
}
else
{
    define('FORUM_URL_PAGED','viewforum.php?forum_id={fid}&amp;page={page}');
}

add_to_head("<link rel=\"stylesheet\" href=\"" . DSB_DIR . "/templates/styles.css\" media=\"screen\"/>");

$gobutton = "<input type=\"submit\" class=\"button\" value=\"".$locale['ok']."\"/>";
?>